package com.gusl.uploadapi.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
//import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.gusl.uploadapi.exception.CommonException;
import com.gusl.uploadapi.vo.UploadedResult;

/**   
*    
* Project：uploadapi   
* Class name：UploadServiceImpl   
* Class Description：   
* Author：gus   
* Create Time：2020年2月14日 下午7:07:51   
* @version        
*/

@Service
public class UploadServiceImpl implements UploadService
{
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    private FastFileStorageClient storageClient;
	
	@Value("${web_domain:localhost}")
    private String uploadapi_domain;
	
	@Value("${web_port:80}")
    private String uploadapi_port;

	@Value("${fdfs.thumbImage.width:60}")
	private String fdfs_thumbImage_width;

	@Override
	public UploadedResult uploadImage(MultipartFile file,boolean isNeedThumb) throws IOException,CommonException
	{
		final List<String> CONTENT_TYPE = Arrays.asList("image/jpeg","image/png","image/gif","image/bmp");
		String contenttype = file.getContentType();
		if(!CONTENT_TYPE.contains(contenttype))
		{
			throw new CommonException("必须是图片文件");
		}
		String suffixName = FilenameUtils.getExtension(file.getOriginalFilename());
		StorePath storePath = null;
		if(isNeedThumb)
		{
			storePath = storageClient.uploadImageAndCrtThumbImage((InputStream)file.getInputStream(),file.getSize(),suffixName,null);
		}
		else
		{
			storePath = storageClient.uploadFile((InputStream)file.getInputStream(),file.getSize(), suffixName, null);
		}
		String path = storePath.getFullPath();
		String fileUrl = getWebUrl(storePath.getFullPath());
		log.info("image path:"+storePath.getFullPath());
		UploadedResult ur = new UploadedResult();
		ur.setCode(0);
		ur.setMsg("OK");
		ur.setFile(fileUrl);
		if(isNeedThumb)
		{
			String thumbpath = path.replaceAll("\\."+suffixName,"_" + fdfs_thumbImage_width + "x" + fdfs_thumbImage_width + "." + suffixName);
			ur.setSmallfile(getWebUrl(thumbpath));
		}
		else
		{
			ur.setSmallfile("");
		}
		return ur;
	}

	@Override
	public UploadedResult deleteFile(String imageurl) throws IOException, CommonException
	{
		String filename = "";
		if(!StringUtils.isEmpty(imageurl))
		{
			String[] group1s = imageurl.split("group1");
			if(group1s.length > 1)
			{
				filename = "group1" + group1s[1];
			}
		}
		log.info("delete file:" + filename);
		storageClient.deleteFile(filename);
		UploadedResult ur = new UploadedResult();
		ur.setCode(0);
		ur.setMsg("OK");
		ur.setFile(imageurl);
		ur.setSmallfile("");
		return ur;
	}

	private String getWebUrl(String storePath)
	{
		if("80".equals(uploadapi_port))
		{
			String fileUrl = "http://" + uploadapi_domain + "/" + storePath;
		    return fileUrl;
		}
		else
		{
	       String fileUrl = "http://" + uploadapi_domain + ":" + uploadapi_port + "/" + storePath;
	       return fileUrl;
		}
	}

}
